package cn.iocoder.yudao.module.train.controller.wechat.teaching;

import cn.hutool.core.bean.BeanUtil;
import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.module.train.controller.admin.coursecancel.vo.CourseCancelSaveReqVO;
import cn.iocoder.yudao.module.train.controller.admin.coursestudentregistform.vo.CourseStudentRegistFormSaveReqVO;
import cn.iocoder.yudao.module.train.controller.admin.scanrecord.vo.ScanRecordSaveReqVO;
import cn.iocoder.yudao.module.train.dal.dataobject.classsetting.ClassSettingDO;
import cn.iocoder.yudao.module.train.dal.dataobject.connectDTO.CourseStudentRegistFormDTO;
import cn.iocoder.yudao.module.train.dal.dataobject.coursecancel.CourseCancelDO;
import cn.iocoder.yudao.module.train.dal.dataobject.courseclasses.CourseClassesDO;
import cn.iocoder.yudao.module.train.dal.dataobject.coursecommodity.CourseCommodityDO;
import cn.iocoder.yudao.module.train.dal.dataobject.coursestudentregistform.CourseStudentRegistFormDO;
import cn.iocoder.yudao.module.train.service.classsetting.ClassSettingService;
import cn.iocoder.yudao.module.train.service.coursecancel.CourseCancelService;
import cn.iocoder.yudao.module.train.service.courseclasses.CourseClassesService;
import cn.iocoder.yudao.module.train.service.coursecommodity.CourseCommodityService;
import cn.iocoder.yudao.module.train.service.coursestudentregistform.CourseStudentRegistFormService;
import cn.iocoder.yudao.module.train.service.scanrecord.ScanRecordService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.List;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.CREATE;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 签到记录")
@RestController
@RequestMapping("/wechat-api/teaching")
public class WechatTeachingController {

    @Resource
    private ScanRecordService scanRecordService;

    @Resource
    private CourseStudentRegistFormService courseStudentRegistFormService;

    @Resource
    private ClassSettingService classSettingService;

    @Resource
    private CourseClassesService courseClassesService;

    @Resource
    private CourseCommodityService courseCommodityService;

    @Resource
    private CourseCancelService courseCancelService;

    @PostMapping("/studentSignin")
    @Operation(summary = "学生上课签到")
    @ApiAccessLog(operateType = CREATE)
    public CommonResult<Integer> studentSignin(ScanRecordSaveReqVO reqVO) {
        ClassSettingDO settingDO = classSettingService.getClassSetting(reqVO.getClassSettingId());
        List<CourseStudentRegistFormDTO> list = courseStudentRegistFormService.getCourseStudentRegistFormListByClassesId(settingDO.getClassesId());
        for (CourseStudentRegistFormDTO form : list) {
            if (form.getStudentId().equals(reqVO.getStudentId())) {
                CourseStudentRegistFormDO validDO = courseStudentRegistFormService.getCourseStudentRegistForm(form.getId());
                CourseStudentRegistFormSaveReqVO validReqVO = BeanUtil.copyProperties(validDO, CourseStudentRegistFormSaveReqVO.class);
                CourseClassesDO classesDO = courseClassesService.getCourseClasses(settingDO.getClassesId());
                CourseCommodityDO commodityDO = courseCommodityService.getCourseCommodity(classesDO.getCommodityId());
                validReqVO.setRestDuration(Math.max(validReqVO.getRestDuration() - commodityDO.getTeachingTime(), 0));
                courseStudentRegistFormService.updateCourseStudentRegistForm(validReqVO);
                CourseCancelSaveReqVO cancelDO = new CourseCancelSaveReqVO();
                cancelDO.setStudentId(validDO.getStudentId());
                cancelDO.setCommodityId(commodityDO.getId());
                cancelDO.setClassesId(classesDO.getId());
                cancelDO.setCancelTime(LocalDateTime.now());
                cancelDO.setTeachingTime(commodityDO.getTeachingTime());
                cancelDO.setRestDuration(Math.max(validReqVO.getRestDuration() - commodityDO.getTeachingTime(), 0));
                courseCancelService.createCourseCancel(cancelDO);
            }
        }
        return success(scanRecordService.createScanRecord(reqVO));
    }

}
